package edu.thu.studio.ormdesigner.editor.dialog;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.Text;

import edu.thu.studio.ormdesigner.common.CompositeFactory;
import edu.thu.studio.ormdesigner.common.InputException;
import edu.thu.studio.ormdesigner.common.ValidatableTabWrapper;
import edu.thu.studio.ormdesigner.util.Properties;
import edu.thu.studio.ormdesigner.util.Utils;

public abstract class AbstractGeneralTabWrapper extends ValidatableTabWrapper {
	
	protected Text codeText;
	protected Text nameText;
	protected Text commentText;
	
	protected Button generateCodeText;
	protected Text stereotypeText;
	
	protected NodeDialog nodeDialog;
	
	public AbstractGeneralTabWrapper(NodeDialog dialog,
			TabFolder parent, String title) {
		super(dialog, parent, title);
		
		this.nodeDialog = dialog;
		
	}

	@Override
	public void validatePage() throws InputException {
		String code = this.codeText.getText().trim();
		String name = this.nameText.getText().trim();

		if (Utils.isNull(code)) {
			throw new InputException(Properties.error_code_empty);
		}
		if (Utils.isNull(name)) {
			throw new InputException(Properties.error_name_empty);
		}
		if (!Utils.isAlphabet(code)) {
			throw new InputException(Properties.error_code4cn);
		}
		if(!this.unqCode()){
			throw new InputException(Properties.error_table_code_unq);
		}
		if(!this.unqName()){
			throw new InputException(Properties.error_table_name_unq);
		}
	}

	@Override
	protected void initComposite() {
		this.setLayout(new GridLayout());

		Composite header = new Composite(this, SWT.NONE);
		GridLayout gridLayout1 = new GridLayout(4, false);
		gridLayout1.horizontalSpacing = 22;
		header.setLayout(gridLayout1);

		this.codeText = CompositeFactory.createText(nodeDialog, header,
				Properties.diagram_label_table_general_code, 1, 500, false,
				true);
		this.generateCodeText = CompositeFactory.createCheckbox(nodeDialog, header,
				Properties.diagram_label_generate_code, false);

		Composite footer = new Composite(this, SWT.NONE);
		GridLayout gridLayout2 = new GridLayout(2, false);
		gridLayout2.horizontalSpacing = 10;
		gridLayout2.verticalSpacing = 15;
		footer.setLayout(gridLayout2);

		this.nameText = CompositeFactory.createText(nodeDialog, footer,
				Properties.diagram_label_table_general_name, 1, 600, false,
				true);
		this.stereotypeText = CompositeFactory.createText(nodeDialog, footer,
				Properties.diagram_label_stereotype, 1, 600, false, true);
		this.commentText = CompositeFactory.createTextArea(nodeDialog, footer,
				Properties.diagram_label_table_general_comment, -1, 200, 1,
				true);
	}

	@Override
	public void perfomeOK() {
	}

	@Override
	public void setInitFocus() {
		this.codeText.setFocus();
	}
	
	protected abstract boolean unqCode();
	
	protected abstract boolean unqName();
	

}
